/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package raytracer;

import java.awt.Color;

/**
 *
 * @author ferco
 * Trida repreyentujici svetlo ve svete, prozatim bodove
 * http://www.ceske-hry.cz/forum/viewtopic.php?t=1420&postdays=0&postorder=asc&start=0
 */
public class Light {

    //deklarace promennzch
    private Vector3D possition;
    private Color color;

    /**
     *Konstruktor s parametrem barvy a pozice svetla
     *@param possition pozice svetla
     *@param color barva svetla
     */
    public Light(Vector3D possition, Color color) {
        this.possition = possition;
        this.color = color;
    }

    /**
     * metoda pro vypocet difuzniho osvetleni
     * @param point     bod na objektu
     * @param normal    normala z tohoto bodu
     * @return          parametr upravujici barvu
     */
    public double calculateDiffuse(Vector3D vectorToLight, Vector3D normal){

        //vytvor vektor ke svetlu a znormalizuj ho
        //Vector3D vectorToLight = this.getPossition().minusVector(point);
        vectorToLight.normalize();
        normal.normalize();
        //vypocitej difuzni osvetleni jako skalarovy soucin vektoru ke
        //svetlu a normaly objektu
        double difuseLight = vectorToLight.dot(normal);
        //uprav barvu paprsku o toto osvetleni        
        return difuseLight;
    }

//    vecteur blinnDir = lightRay.dir - viewRay.dir;
// float temp = sqrtf( blinnDir * blinnDir);
// if (temp != 0.0f )
// {
//   blinnDir = (1.0f / temp) * blinnDir;
//   float blinnTerm = _MAX(blinnDir * n, 0.0f);
//   blinnTerm = currentMat.specvalue * powf(blinnTerm , currentMat.specpower) * coef;
//
//   red += blinnTerm * current.red ;
//   green += blinnTerm * current.green ;
//   blue += blinnTerm * current.blue;
// }

    public double calculateSpecular(Ray ray, Vector3D vectorToLight, Vector3D normal){

       // Vector3D vectorToLight = this.getPossition().minusVector(point);
        //Vector3D vectorToLight = point.minusVector(this.getPossition());
        vectorToLight.normalize();
        Vector3D reflectedVTL = ray.getDirection().reflectVector(normal);
        reflectedVTL.normalize();
       // ray.getDirection().normalize();
        double dot = reflectedVTL.dot(vectorToLight);

        if(dot > 0){
            double spec = Math.pow(dot, 400);
            return spec;
        }

        return 0;
    }

    public double calculateBlinnPhong(Vector3D point, Vector3D normal){

        double spec = 1;
        Vector3D specV = new Vector3D(1, 1, 1);
        double specpow = 60;
        double coef = 0.5;
        double blinnTerm = 0;

        Vector3D vectorToLight = this.getPossition().minusVector(point);
        double temp = Math.sqrt(vectorToLight.dot(vectorToLight));
        if(temp != 0){
            
            vectorToLight = vectorToLight.multiplyBy(1/ temp);
            blinnTerm = Math.max(vectorToLight.dot(normal), 0);
            blinnTerm = spec * Math.pow(blinnTerm, specpow) * coef;

        }
        System.out.println(blinnTerm);
        return blinnTerm;
    }

    public double calculateShadow(Ray shadowRay, Scene scene){

        Intersection intersection;
        for (Primitive p : scene.getPrimitives()) {
            intersection = p.findIntersection(shadowRay);
            if (intersection != null) {
               return 0.5;
            }
        }
        return 1;
    }
    
    public Color getColor() {
        return color;
    }

    public Vector3D getPossition() {
        return possition;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public void setPossition(Vector3D possition) {
        this.possition = possition;
    }




}
